package com.zhou.yygh.oss.service.impl;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.zhou.yygh.oss.service.FileUploadService;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Value("${oss.path.upload}")
    private String uploadPath;
    @Override
    public String upload(MultipartFile file) {
        String url = uploadPath + "/"+generateUUID()+file.getName()+".jpg";
        File fileName = new File(url);

        try {
            InputStream inputStream = file.getInputStream();

            file.transferTo(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }
    //生成随机字符串
    public static String generateUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

}
